import re

def process_python_file(file_path):
    """Process Python files to extract function relationships including __main__ block"""
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()
    
    # Parse the Python file
    chunks = []
    file_aliases = {}
    
    # Add regular functions and methods
    # ... existing parsing code ...
    
    # Special handling for if __name__ == "__main__" block
    main_block_match = re.search(r'if\s+__name__\s*==\s*[\'"]__main__[\'"]\s*:', content)
    if main_block_match:
        # Extract the main block and create a virtual function for it
        main_start = main_block_match.start()
        # Find the indented block after the if statement
        main_block = extract_main_block(content[main_start:])
        
        # Create a chunk for the main block
        main_chunk = {
            'type': 'function',
            'name': '__main_block__',  # Virtual name for the main block
            'file': file_path,
            'content': main_block,
            'calls': extract_function_calls(main_block)
        }
        chunks.append(main_chunk)
        
        # Add relationship between main function and __main_block__
        for chunk in chunks:
            if chunk['name'] == 'main':
                if 'called_by' not in chunk:
                    chunk['called_by'] = []
                chunk['called_by'].append('__main_block__')
    
    return chunks, file_aliases

def extract_main_block(content):
    """Extract the indented block following the if __name__ == "__main__" statement"""
    lines = content.split('\n')
    block_lines = []
    in_block = False
    indent_level = None
    
    for line in lines:
        if not in_block and 'if __name__ == "__main__"' in line:
            in_block = True
            continue
        
        if in_block:
            if line.strip() == '' or line.strip().startswith('#'):
                block_lines.append(line)
                continue
                
            # Determine indent level on first non-empty line
            if indent_level is None:
                indent_match = re.match(r'^(\s+)', line)
                if indent_match:
                    indent_level = len(indent_match.group(1))
                else:
                    # No indent on first line means block is incorrectly formatted
                    break
            
            # Check if we've exited the block (less indentation)
            if line.strip() and not line.startswith('#'):
                current_indent = len(re.match(r'^(\s*)', line).group(1))
                if current_indent < indent_level:
                    break
                    
            block_lines.append(line)
    
    return '\n'.join(block_lines)

def extract_function_calls(content):
    """Extract function calls from a code block"""
    # Basic regex to find function calls
    calls = re.findall(r'(\w+)\s*\(', content)
    # Filter out common Python functions and keywords
    filtered_calls = [call for call in calls if call not in ['print', 'open', 'if', 'for', 'while']]
    return filtered_calls